package com.maple.exclusivesport;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.widget.Toast;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by maple on 2019/7/15 16:22
 * E-Mail Address：740917401@qq.com
 */
public class CameraColour implements Callback {
    private SurfaceHolder surfaceHolder;
    private Camera mCamera;

    public CameraColour(SurfaceHolder surfaceHolder) {
        this.surfaceHolder = surfaceHolder;
        surfaceHolder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        startPreview();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        stopPreview();
    }


    public void takeFrame(byte[] data) {
        Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        mBitmap = FileTool.rotaingImageView(Constants.cameraIndex == 1 ? - Constants.displayOrientation : Constants.displayOrientation, mBitmap);
        Constants.filePath = Constants.CACTH_PHOTO_PATH + UUID.randomUUID().toString() + ".jpeg";
        File file = new File(Constants.filePath);
        try {
            file.createNewFile();
            BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 40, os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void takePicture() {
        mCamera.takePicture(null, null, (data, camera) -> {
            takeFrame(data);

        });
    }

    private byte[] cameraPreviewBuffer;

    /**
     * 开始预览
     */
    public void startPreview() {
        try {
            if (Camera.getNumberOfCameras() >= Constants.cameraIndex + 1) {
                mCamera = Camera.open(Constants.cameraIndex);
                mCamera.setPreviewDisplay(surfaceHolder);
                mCamera.setDisplayOrientation(Constants.displayOrientation);
                mCamera.startPreview();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放内存 防止切换摄像头导致内存泄漏
     *
     * @author maple
     * @time 2019/10/8 10:17
     */
    public void releaseCameraPreviewBuffer() {
        if (cameraPreviewBuffer != null)
            cameraPreviewBuffer = null;
    }

    /**
     * 停止预览
     */
    public void stopPreview() {
        try {
            if (mCamera != null) {
                mCamera.stopPreview();
                mCamera.release();
                releaseCameraPreviewBuffer();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
